package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.TTcProductTypeMapper;
import com.zhijian.medical.entity.po.TTcProductType;
import com.zhijian.medical.entity.vo.request.TTcProductTypeSearchVo;
import com.zhijian.medical.service.TTcProductTypeService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 产品类型表业务处理层
 *
 * @author HCG
 * @date 2021-10-15 15:45:27
 * @version: 1.0
 */
@Slf4j
@Service
public class TTcProductTypeServiceImpl implements TTcProductTypeService {

    @Autowired
    private TTcProductTypeMapper mapper;

    @Override
    public TTcProductType selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(TTcProductType tcProductType) {
        Date now = new Date();
        tcProductType.setCreateTime(now);
        tcProductType.setUpdateTime(now);
        return mapper.insert(tcProductType) == 1 ? tcProductType.getId() : 0L;
    }

    @Override
    public boolean update(TTcProductType tcProductType) {
        Date now = new Date();
        tcProductType.setUpdateTime(now);
        return mapper.updateByPrimaryKey(tcProductType) != 1;
    }

    @Override
    public Integer count(TTcProductTypeSearchVo tcProductTypeSearchVo) {
        return mapper.count(tcProductTypeSearchVo);
    }

    @Override
    public List<TTcProductType> selectList(TTcProductTypeSearchVo tcProductTypeSearchVo) {
        return mapper.selectList(tcProductTypeSearchVo);
    }

    @Override
    public Page<TTcProductType> selectPage(TTcProductTypeSearchVo tcProductTypeSearchVo) {
        //先直接查总数
        int count = count(tcProductTypeSearchVo);
        //分页
        Page<TTcProductType> page = new Page<>(true, count, tcProductTypeSearchVo.getPageNum(), tcProductTypeSearchVo.getPageSize());
        tcProductTypeSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TTcProductType> responseVos = this.selectList(tcProductTypeSearchVo);
        page.setData(responseVos);
        return page;
    }

}